/*
 * Copyright (C) 2009, 2010, B3log Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.b3log.nide.service;

import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.b3log.latke.service.ServiceException;
import org.b3log.nide.model.source.java.JavaMethod;
import org.b3log.nide.util.parser.java.NideJavaParser;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Java code assistant.
 *
 * <ul>
 *   <li>Code completion tip</li>
 *   <li>Compilation diagnostics</li>
 * </ul>
 *
 * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
 * @version 1.0.0.5, Nov 29, 2010
 */
// TODO: refactor CompileService
public final class JavaCodeAssistant {

    /**
     * Logger.
     */
    private static final Logger LOGGER = 
            Logger.getLogger(JavaCodeAssistant.class.getName());
    /**
     * Instance.
     */
    public static final JavaCodeAssistant INSTANCE = new JavaCodeAssistant();

    /**
     * Gets all public methods of the type of variable or method returned before
     * the specified period location for the specified Java source code.
     *
     * @param javaSourceCode the specified Java source code
     * @param lineNum line number of period location
     * @param columnNum column number of period location
     * @return if found the type of variable or method return before the
     * specified period location, returns the public methods of the type,
     * {@code null} otherwise. The element of returned json array see
     * {@linkplain JavaMethod#transform(java.lang.reflect.Method)} for details
     * @throws ServiceException service exception
     */
    public JSONArray getMethods(final String javaSourceCode,
                                final int lineNum, final int columnNum)
            throws ServiceException {
        // TODO: IoC
        final NideJavaParser javaParser = new NideJavaParser(javaSourceCode);
        final Method[] methods = javaParser.getMethods(lineNum, columnNum);

        final JSONArray ret = new JSONArray();

        if (null != methods) {
            try {
                for (final Method method : methods) {
                    final JSONObject methodModel = JavaMethod.transform(method);
                    ret.put(methodModel);
                }
            } catch (final JSONException e) {
                LOGGER.log(Level.SEVERE, e.getMessage(), e);
                throw new ServiceException(e);
            }
        }

        return ret;
    }
}
